// pages/fabushop/fabushop.js
const db = wx.cloud.database().collection('shichang')
const db2 = wx.cloud.database().collection('dating')
const db3 = wx.cloud.database().collection('play')
Page({

  data: {
    imageUrl: '',
    tempFilePaths: '',
    content: '',
    qian: '',
    title: '',
    active: '',
    index: ''
  },

  onLoad: function (options) {
    this.setData ({
      active:options.active,
      index: options.index
    })
  },
  handleinput (data) {
    this.setData ({
      qian: data.detail.value
    })
  },
  hadletitle (data) {
    this.setData ({
      title: data.detail.value
    })
  },
  handletext (data) {
    this.setData ({
      content: data.detail.value
    })
  },
  upImage () {
    wx.showLoading({
      title: "上传中...." ,
      mask: true,
  })
    let thiss = this
    wx.chooseImage({
      count: 4,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        thiss.setData ({
          tempFilePaths: tempFilePaths
        })
        wx.cloud.uploadFile({
          cloudPath: thiss.data.title, // 上传至云端的路径
          filePath: tempFilePaths[0], // 小程序临时文件路径
          success: res => {
            console.log(res)
            thiss.setData ({
              imageUrl: res.fileID
            })
            wx.hideLoading()
          },
          fail: console.error
        })
      }
    })
  },
  addData () { // 添加数据
    wx.showLoading({
      title: "排队中...." ,
      mask: true,
    })
    if (this.data.active === '2') {
      db2.add({
        data: {
          content: this.data.content,
          jianzhiname: this.data.title,
          jifen: this.data.qian,
          renshiname: wx.getStorageSync("userInfo").nickName,
          active: 'no'
        },
        success (res) {
          wx.navigateBack({
            delta: 1
        })
        },
        fail (err) {
          console.log(err)
        }
      })
    } else if (this.data.active === 'play') {
      db3.add({
        data: {
          content: this.data.content,
          image: this.data.imageUrl,
          qian: this.data.qian,
          title: this.data.title,
          user: wx.getStorageSync("userInfo").nickName
        },
        success (res) {
          wx.navigateBack({
            delta: 1
          })
        },
        fail (err) {
          console.log(err)
        }
      })
    } else {
      db.add({
        data: {
          content:this.data.content,
          image:this.data.imageUrl,
          qian:this.data.qian,
          user:wx.getStorageSync("userInfo").nickName
        },
        success (res) {
          wx.navigateBack({
            delta: 1
          })
        },
        fail (err) {
          console.log(err)
        }
      })
    }
  },
})